package com.itheima.service;

import com.itheima.common.R;
import com.itheima.domain.ShoppingCart;
import com.itheima.mapper1.ShoppingCartMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.net.HttpCookie;
import java.security.PublicKey;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author wuyiqiang
 * @2022/6/18 13:39
 */
@Service
public class ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询购物车
     *
     * @return
     */
    public List<ShoppingCart> select() {
        return shoppingCartMapper.select();
    }

    /**
     * 添加商品
     *
     * @param shoppingCart
     * @return
     */
    public R add(ShoppingCart shoppingCart) {

        //先查询购物车中是否有该商品
        ShoppingCart shoppingCart1 = shoppingCartMapper.selectDisOrSid(shoppingCart);

        //获取当前时间
        SimpleDateFormat sdf = new SimpleDateFormat();
        sdf.applyPattern("yyyy-MM-dd HH:mm:ss a");
        Date date = new Date();

        String id = System.currentTimeMillis() + "";

        //如果没有，添加改菜品或者套餐
        if (shoppingCart1 == null) {
            shoppingCart.setId(id);
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(date);
            shoppingCartMapper.add(shoppingCart);
            shoppingCart1 = shoppingCart;
        } else {
            //如果已经有了，添加数量就可以
            shoppingCart1.setNumber(shoppingCart1.getNumber() + 1);
            shoppingCartMapper.update(shoppingCart1);
        }

        System.out.println(shoppingCart1 + "=========================debug1");

        return R.success(shoppingCart1);
    }

    /**
     * 清空购物车
     *
     * @param userId
     */
    public int delete(String userId) {
        return shoppingCartMapper.delete(userId);
    }

    /**
     * 删除套餐或者菜品
     *
     * @return
     */
    public R deleteById(ShoppingCart shoppingCart, HttpServletRequest request) {
        //查询当前菜品所剩数量
        String userId = tokenToUsername(request);

        System.out.println(userId + "====================debug");
        shoppingCart.setUserId(userId);
        ShoppingCart shoppingCart1 = shoppingCartMapper.selectDisOrSid(shoppingCart);
        int num = shoppingCart1.getNumber();
        //判断数量菜品数量是否小于1，不是就更改数量，是的话直接删除该菜品
        if (num <= 1) {
            shoppingCartMapper.deleteById(shoppingCart1);
        } else {
            shoppingCart1.setNumber(num - 1);
            shoppingCartMapper.update(shoppingCart1);
        }

        return R.success(shoppingCart1);

    }


    /**
     * 利用token获得当前页面用户名
     *
     * @param request
     * @return
     */
    public String tokenToUsername(HttpServletRequest request) {
        //用户id设置
        String token = request.getHeader("AuthorizationUser");

        String username = (String) redisTemplate.opsForValue().get(token);


        return username;
    }
}
